package com.ds.home.proxy;

import com.alibaba.fastjson.annotation.JSONField;
import com.ds.home.EIArea;
import com.ds.home.engine.AppEngine;
import com.ds.iot.Area;
import com.ds.iot.HomeException;
import com.ds.iot.Place;
import com.ds.iot.ZNode;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class AreaProxy implements Area {

    private final String systemCode;

    @JSONField(serialize = false)
    private EIArea dbArea;

    public AreaProxy(EIArea dbArea, String systemCode) {
        this.dbArea = dbArea;
        this.systemCode = systemCode;
    }

    public String getAreaid() {
        return dbArea.getAreaid();
    }

    public String getMemo() {
        return dbArea.getMemo();
    }

    public String getName() {
        return dbArea.getName();
    }

    @JSONField(serialize = false)

    public Place getPlace() {
        Place place = null;
        try {
            place = AppEngine.getEngine(systemCode).getPlaceById(this.getPlaceid());
        } catch (HomeException e) {
            e.printStackTrace();
        }
        return place;
    }

    public String getPlaceid() {
        return dbArea.getPlaceid();
    }

    public void setAreaid(String areaid) {
        dbArea.setAreaid(areaid);
    }

    public void setMemo(String memo) {
        dbArea.setMemo(memo);
    }

    public void setName(String name) {
        dbArea.setName(name);
    }

    public void setPlaceid(String placeid) {
        dbArea.setPlaceid(placeid);
    }

    @Override
    public Set<String> getSensorIds() {
        Set<String> sensors = new LinkedHashSet<>();
        try {
            sensors = AppEngine.getEngine(systemCode).getAllZNodeIdsByAreaId(this.getAreaid());
        } catch (HomeException e) {
            e.printStackTrace();
        }
        return sensors;
    }

    @JSONField(serialize = false)
    public List<ZNode> getSensors() {
        List<ZNode> sensors = new ArrayList<ZNode>();
        try {
            sensors = AppEngine.getEngine(systemCode).getAllZNodeByAreaId(this.getAreaid());
        } catch (HomeException e) {
            e.printStackTrace();
        }
        return sensors;

    }

}
